package com.ruoyi.store.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.store.mapper.StockInDetailMapper;
import com.ruoyi.store.mapper.StockInMapper;
import com.ruoyi.store.mapper.StockOutDetailMapper;
import com.ruoyi.store.mapper.StockOutMapper;
import com.ruoyi.store.service.IStockOutDetailService;
import com.ruoyi.system.api.domain.store.*;
import com.ruoyi.xiaoshou.api.RemoteSaleBackRequestDetailService;
import com.ruoyi.xiaoshou.api.RemoteSaleBackRequestService;
import com.ruoyi.xiaoshou.api.RemoteSaleOrderDetailService;
import com.ruoyi.xiaoshou.api.RemoteSaleOrderService;
import com.ruoyi.xiaoshou.domain.SaleBackRequest;
import com.ruoyi.xiaoshou.domain.SaleBackRequestDetail;
import com.ruoyi.xiaoshou.domain.SaleOrder;
import com.ruoyi.xiaoshou.domain.SaleOrderDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 罗梦杰
 * date 2024/10/25 19:50
 * @version 1.0
 */
@Service
@Slf4j
@Transactional
public class StockOutDetailServiceImpl implements IStockOutDetailService {
    @Autowired
    private StockOutDetailMapper stockOutDetailMapper;
    @Autowired
    private StockOutMapper stockOutMapper;
    @Autowired
    private RemoteSaleBackRequestService remoteSaleBackRequestService;
    @Autowired
    private RemoteSaleBackRequestDetailService remoteSaleBackRequestDetailService;
    @Autowired
    private RemoteSaleOrderService remoteSaleOrderService;
    @Autowired
    private RemoteSaleOrderDetailService saleOrderDetailService;
    @Autowired
    private StockInMapper stockInMapper;
    @Autowired
    private StockInDetailMapper stockInDetailMapper;

    @Override
    public List<StockOutDetail> getOutStockDetail(int stockOutId) {
        return stockOutDetailMapper.getOutStockDetail(stockOutId);
    }

    @Override
    public List<StockOutDetail> getOutDetailByOutId(int outId) {
        log.info("StockOutDetailServiceImpl getOutDetailByOutId params:{}", outId);
        //获取可以入库的调拨出库列表，获得入库单关联的出库单编号，根据出库单编号查询出库单，根据
        //出库单id查询出库详情，根据商品id来寻找商品来计算数量
        List<StockOutDetail> stockOutDetails = stockOutDetailMapper.getOutDetailByOutId(outId);
        StockOut stockOut = stockOutMapper.getStockOutById(outId);
        String code = stockOut.getStockOutCode();
        StockIn stockIn = new StockIn();
        stockIn.setOrderCode(code);
        List<StockIn> stockIns = stockInMapper.selectStockInList(stockIn);
        if (stockIns.size() == 0 || stockIns == null) {
            return stockOutDetails;
        }
        Integer[] idArray = stockIns.stream().map(StockIn::getId).toArray(Integer[]::new);
        List<StockInDetail> stockInDetails = stockInDetailMapper.getStockInDetailByIds(idArray);
        //如果有详情就继续
        if (stockInDetails.isEmpty()) {
            return stockOutDetails;
        }
        List<StockOutDetail> stockOutDetailList = new ArrayList<>();
        for (StockOutDetail stockOutDetail : stockOutDetails) {
            int stockOutNumber = stockOutDetail.getStockOutNumber();
            for (StockInDetail stockInDetail1 : stockInDetails) {
                if (stockInDetail1.getGoodsId() == stockOutDetail.getGoodsId()) {
                    stockOutNumber -= stockInDetail1.getTotalNum();
                }
            }
            if (stockOutNumber > 0) {
                stockOutDetail.setStockOutNumber(stockOutNumber);
                stockOutDetailList.add(stockOutDetail);
            }
        }
        return stockOutDetailList;
    }

    @Override
    public List<StockOutDetail> getStockOutDetailUseList(StockOutDetail stockOutDetail) {
        log.info("StockOutDetailServiceImpl getStockOutDetailUseList params:{}", stockOutDetail);
        //获取可以使用的退货列表，根据出库id获取出库详情，根据出库id查询出库单，
        //根据出库单编号查询关联的退货申请单编号数组，根据数组查询详情列表，详情列表根据proCode减去数量
        List<StockOutDetail> stockOutDetails = stockOutDetailMapper.getOutStockDetail(stockOutDetail.getStockOutId());
        StockOut stockOut = stockOutMapper.getStockOutById(stockOutDetail.getStockOutId());
        if (stockOut.getId() == null) {
            log.error("getStockOutDetailUseList getStockOutById fail");
            throw new RuntimeException("获取出库单错误");
        }
        //获取销售金额，先根据订单code获取订单，然后获取订单id，然后根据订单id和商品id获取商品价格
        String orderCode = stockOut.getOrderCode();
        SaleOrder saleOrder = remoteSaleOrderService.getOrderByCode(orderCode);
        int orderId = saleOrder.getId();
        R<List<SaleOrderDetail>> resultDetail = saleOrderDetailService.getSaleOrderDetail(orderId);
        List<SaleOrderDetail> saleOrderDetails = resultDetail.getData();
        for (StockOutDetail stockOutDetail1 : stockOutDetails) {
            for (SaleOrderDetail saleOrderDetail : saleOrderDetails) {
                if (saleOrderDetail.getProId() == stockOutDetail1.getGoodsId()) {
                    stockOutDetail1.setPrice(saleOrderDetail.getPrice());
                    break;
                }
            }
        }
        R<List<SaleBackRequest>> result = remoteSaleBackRequestService.getSaleBackRequestListBySaleOutCode(stockOut.getStockOutCode());
        List<SaleBackRequest> saleBackRequests = result.getData();
        //如果申请单有对应绑定的退货订单就继续
        if (!(saleBackRequests.size() > 0) || saleBackRequests == null) {
            return stockOutDetails;
        }
        // 获取 SaleBackCode 数组
        // 将 List 转换为数组
        String[] codeArray = saleBackRequests.stream().map(SaleBackRequest::getSaleBackRequestCode).toArray(String[]::new);
        List<SaleBackRequestDetail> saleBackRequestDetails = remoteSaleBackRequestDetailService.getSaleBackRequestDetailByCodes(codeArray).getData();
        //如果有详情就继续
        if (!(saleBackRequestDetails.size() > 0) || saleBackRequestDetails == null) {
            return stockOutDetails;
        }
        //最终结果
        List<StockOutDetail> stockOutDetailList = new ArrayList<>();
        for (StockOutDetail stockOutDetail1 : stockOutDetails) {
            int num = stockOutDetail1.getNum();
            for (SaleBackRequestDetail saleBackRequestDetail : saleBackRequestDetails) {
                if (saleBackRequestDetail.getProCode().equals(stockOutDetail1.getProCode())) {
                    num = num - saleBackRequestDetail.getProNum();
                }
            }
            if (num > 0) {
                stockOutDetail1.setNum(num);
                stockOutDetailList.add(stockOutDetail1);
            }
        }
        return stockOutDetailList;
    }
}
